<?php

namespace app\api\controller;

use app\admin\model\user\UserApplyGoods as UserApplyGoodsModel;
use app\admin\model\user\UserGoods;
use app\admin\model\litestore\Litestoregoods;
use app\common\controller\Api;
use think\Db;

/**
 * 博主申请商品
 */
class UserApplyGoods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $user_id = '';

    public function _initialize()
    {
        parent::_initialize();
        //游客不允许请求
        $user_info = $this->auth->getUser();
        if(empty($user_info)){
            return $this->error('Please login');
        }
        $this->user_id = $user_info['id'];
    }

    /**
     * 我的售卖商品
     */
    public function index()
    {
        $page = $this->request->request('page');

        // 筛选条件
        $filter['user_id'] = $this->user_id;

        $model_temp = new UserGoods();
        $listdata = $model_temp
            ->with(['goods'=>function($query){
                return $query->withField('goods_name,images,goods_status');
            }])
            ->where($filter)->where('deletetime is null')->order('id desc')
            ->paginate(8, false, [ 'page' => $page ])
            ->each(function($item){
                $item->goods->images = cdnurl(explode(",",$item['goods']['images'])[0], true);
                $sku_money = Db::name('litestore_goods_spec')->where('goods_id',$item->goods_id)->value('goods_price');
                $item->goods_price = $sku_money;
                $item->blogger_money = round($item->blogger_rate/100 * $sku_money,2);
            });

        $pagedata = $listdata;
        $this->success('', [
            'pagedata' => $pagedata,
        ]);
    }

    /**
     * 删除我的博主商品
     */
    public function del_my_goods()
    {
        $params = $this->request->request();

        $model_temp = new UserGoods();
        $row = $model_temp->get($params['id']);
        $row->delete();

        $this->success('success');
    }

    /**
     * 新增博主商品时，选择商品的接口
     */
    public function get_goods_list()
    {
        $page = $this->request->request('page');

        //该博主商品库中已有商品
        $model_temp = new UserGoods();
        $blogger_goods_ids = $model_temp->where('user_id',$this->user_id)->column('goods_id');

        // 筛选条件
        $filter['goods_status'] = '10';
        $filter['is_delete'] = '0';
        $model_temp = new Litestoregoods();
        $listdata = $model_temp->field('goods_id,goods_name,images')
            ->where($filter)->order('goods_id desc')
            ->paginate(8, false, [ 'page' => $page ])
            ->each(function($item) use($blogger_goods_ids){
                $item->images = cdnurl(explode(",",$item['images'])[0], true);
                $item->is_select = 0;
                if(in_array($item['goods_id'],$blogger_goods_ids)){
                    $item->is_select = 1;
                }
                $goods_price = Db::name('litestore_goods_spec')->where('goods_id',$item->goods_id)->value('goods_price');
                $item->goods_price = $goods_price;
            });

        $pagedata = $listdata;
        $this->success('', [
            'pagedata' => $pagedata,
        ]);
    }

    /**
     * 申请商品列表（显示申请id、商品数量、状态、有审核信息显示审核信息、申请时间、审核时间）
     */
    public function apply_goods_list()
    {
        $page = $this->request->request('page');

        // 筛选条件
        $filter['user_id'] = $this->user_id;
        $model_temp = new UserApplyGoodsModel();
        $listdata = $model_temp
            ->where($filter)->order('id desc')
            ->paginate(8, false, [ 'page' => $page ])
            ->each(function($item){
                $item->goods_count = count(explode(',',$item->goods_ids));
            });

        $pagedata = $listdata;
        $this->success('', [
            'pagedata' => $pagedata,
        ]);
    }

    /**
     * 新建申请商品后 是待审核状态
     */
    public function add_apply_goods()
    {
        $params = $this->request->request();

        //该博主商品库中已有商品
        $model_temp = new UserGoods();
        $blogger_goods_ids = $model_temp->where('user_id',$this->user_id)->column('goods_id');

        $goods_ids = explode(',',$params['goods_ids']);
        foreach ($goods_ids as $k=>$v){
            if(in_array($v,$blogger_goods_ids)){
                $this->error('Goods：'.$v.', already exists');
            }
        }

        $data['user_id'] = $this->user_id;
        $data['goods_ids'] = $params['goods_ids'];

        $model_temp = new UserApplyGoodsModel();
        $model_temp->save($data);

        $this->success('Apply success');
    }

    /**
     * 详情编辑：显示申请的商品
     */
    public function edit_apply_goods()
    {
        $params = $this->request->request();

        $model_temp = new UserApplyGoodsModel();
        $row = $model_temp->get($params['id']);

        //获取详情
        if ($params['act'] == 'info') {
            $goods = (new Litestoregoods())::all(['goods_id'=>['in',$row['goods_ids']]],['spec']);
            $goods_info = [];
            foreach($goods as $k=>$v){
                $goods_price = Db::name('litestore_goods_spec')->where('goods_id',$v['goods_id'])->value('goods_price');
                $goods_info[$k]['goods_id'] = $v['goods_id'];
                $goods_info[$k]['goods_name'] = $v['goods_name'];
                $goods_info[$k]['pro_no'] = $v['pro_no'];
                $goods_info[$k]['goods_price'] = intval($goods_price);
                $goods_info[$k]['images'] = cdnurl(explode(",",$v['images'])[0], true);
            }
            $row['goods_info'] = array_values($goods_info);
            $this->success('success',$row);
        }

        if($row['state'] != 4){
            $this->error('Status error');
        }

        //该博主商品库中已有商品
        $model_temp = new UserGoods();
        $blogger_goods_ids = $model_temp->where('user_id',$this->user_id)->column('goods_id');

        $goods_ids = explode(',',$params['goods_ids']);
        foreach ($goods_ids as $k=>$v){
            if(in_array($v,$blogger_goods_ids)){
                $this->error('Goods：'.$v.', already exists');
            }
        }

        $data['goods_ids'] = $params['goods_ids'];
        $row->save($data);

        $this->success('success');
    }

    /**
     * 撤销-待审核状态可以撤销
     */
    public function cancel_apply_goods()
    {
        $params = $this->request->request();

        $model_temp = new UserApplyGoodsModel();
        $row = $model_temp->get($params['id']);

        if($row['state'] != 1){
            $this->error('Status error');
        }

        $data['state'] = '4';
        $row->save($data);

        $this->success('success');
    }

    /**
     * 提交审核 - 撤销状态 可删除、新增后，提交审核
     */
    public function submit_apply_goods()
    {
        $params = $this->request->request();

        $model_temp = new UserApplyGoodsModel();
        $row = $model_temp->get($params['id']);

        if($row['state'] != 4){
            $this->error('Status error');
        }

        $data['state'] = '1';
        $row->save($data);

        $this->success('success');
    }
}
